import 'dart:async';

import 'package:args/command_runner.dart';
import 'package:gate_cli/models/res_config.dart';
import 'package:gate_cli/utils/command_util.dart';
import 'package:gate_cli/utils/intl_command_util.dart';
import 'package:gate_cli/utils/intl_view/run_intl_view.dart';
import 'package:gate_cli/utils/language_util.dart';

class IntlCommand extends Command<String> {
  IntlCommand() {
    argParser.addFlag('analyse', abbr: 'a', help: '国际化文件分析', defaultsTo: false);
    argParser.addFlag('translate', abbr: 't', help: '国际化文件简繁翻译', defaultsTo: false);
    argParser.addCommand('view');
    argParser.addOption('config', abbr: 'c', help: '配置文件');
  }

  @override
  String get description => '国际化工具';

  @override
  String get name => 'intl';

  @override
  List<String> get aliases => ['i'];

  @override
  FutureOr<String>? run() {
    if (argResults?.arguments.contains('view') ?? false) {
      return IntlViewCommandUtil.run();
    }

    final isAnalyse = argResults?['analyse'] ?? false;
    final translate = argResults?['translate'] ?? false;
    if (translate) {
      LanguageUtil.translationZhHant();
      return "";
    }
    bool emptyCommand = !isAnalyse;
    if (emptyCommand) {
      print('\u001b[31m 请输入正确指令，或使用-h查看帮助 \u001b[0m');
      return "";
    }
    if (isAnalyse) {
      analyse();
    }
    return "";
  }

  void analyse() {
    final config = CommandUtil.loadConfig(argResults?['config'] ?? "");
    final resConfig = ResConfig.fromYaml(config);
    final result = IntlCommandUtil.instance.analyse(resConfig.resIntl ?? ResIntl());
    print(result);
    return;
  }
}
